/*
 * eventhandler.h
 * This file is part of OpenAnimator
 *
 * Copyright (C) 2008 - Abhinav Lele
 *
 * OpenAnimator is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * OpenAnimator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenAnimator; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */
 
 
#ifndef __OA_EVENTHANDLER_H__
#define __OA_EVENTHANDLER_H__

#include "common.h"

#include "lua_environment.h"

enum EVENTTYPE { 
		TYPE_KEYBOARD		=  1	,
		TYPE_MOUSEBUTTON 	=  2	,
		TYPE_MOUSEMOVE   	=  4	,
		TYPE_WMQUIT         =  8	,
		TYPE_WMRESIZE		=  16	,
		TYPE_WMEXPOSE       =  32	};
		
enum BUTTONSTATE { KEYPRESSED = 0x10000, KEYRELEASED = 0x20000 , MOUSEPRESSED = 0x40000 ,MOUSERELEASED = 0x80000 };

/* event num [ -buttonstate-           EVENT    ]
 *            <-- 16 bits --> | <-- 16 bits -->
 *            buttonstate = [ 00 00 mouse keybd ]
 */
// !!!! struct event_repo_object { vector<string> objects; vector<string> fncallname; };
class EventKeeper {
	public:
		map< unsigned long, map<string, string, ltstr_stl>* > repository; 
		/**< fingerprint -> list of [ objectname , callfnname ] */

		LuaEnvironment script_land;
		EventKeeper() {}
		~EventKeeper(); 
		inline void set_event(unsigned long fingerprint, string objectname, string fnname) {
			T("EventKeeper:set_event: " << fingerprint << " " << objectname << " " << fnname )
			map< unsigned long, map<string, string, ltstr_stl>* >::iterator tempiter;
			map<string,string,ltstr_stl> * ptr = NULL;	
			fingerprint &= 0xFFFFFFFF;
			tempiter = repository.find(fingerprint);
			
			if(tempiter == repository.end()) { 
				ptr = new map<string,string,ltstr_stl>();
				if(!ptr) return;
				repository[fingerprint] = ptr;
			}else {
				ptr = tempiter->second;
			}
			(*ptr)[objectname] = fnname; /* update/set the new mapping */
		}
		inline int LUA_set_event(lua_State *L) {
			T("EventKeeper:LUA_set_event")
			if(!(lua_isnumber(L,ARG3_1) && lua_isstring(L,ARG3_2) && lua_isstring(L,ARG3_3))) {
				E("EventKeeper:LUA_set_event: arguments invalid")
				return 0; // arg validation fail 
			}
			unsigned long fingerprint = ((unsigned long)lua_tonumber(L,ARG3_1)) & 0xFFFFFFFF;
			M("EventKeeper:LUA_set_event: fingerprint (dec) " << std::dec << fingerprint << " (hex) " << std::hex << fingerprint << std::dec)
			set_event(fingerprint, string(lua_tostring(L,ARG3_2)),string(lua_tostring(L,ARG3_3)));
			return 1;
		}
};

class EventChecker {
	public:
		EventChecker(){}
		~EventChecker() {} 
		void do_events();
};

#endif
